﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using PearTunes.Model;
using System.Data.SqlServerCe;

namespace PearTunes.DAL
{
    /// <summary>
    /// Stores the data entities
    /// </summary>
    public class PearTunesEntities : DbContext
    {
        /// <summary>
        /// Stores the user entities
        /// </summary>
        public DbSet<User> Users { get; set; }
        /// <summary>
        /// Stores the album entities
        /// </summary>
        public DbSet<Album> Albums { get; set; }
        /// <summary>
        /// Stores the track entities
        /// </summary>
        public DbSet<Track> Tracks { get; set; }
        private static int _hihgestUserId = 0;
        private static int _hihgestAlbumId = 0;
        private static int _hihgestTrackId = 0;

        /// <summary>
        /// Creates a new PearTunesEntities class with default connection to slq server ce 4.0 and with lazy loading disabled
        /// </summary>
        public PearTunesEntities()
        {
            System.Data.Entity.Database.DefaultConnectionFactory = new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
            this.Configuration.LazyLoadingEnabled = false;
            
        }
        /// <summary>
        /// Runs when the database model is created
        /// </summary>
        /// <param name="modelBuilder">The object to create the database model with</param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Album>().HasMany(a => a.Tracks).WithMany(t => t.Albums);
            // modelBuilder.Entity<Album>();

        }
        /// <summary>
        /// Generats a unique user ID
        /// </summary>
        /// <returns>Unique user id</returns>
        public int GenerateUserId()
        {
            System.Threading.Interlocked.Increment(ref _hihgestUserId);
            return _hihgestUserId;
        }
        /// <summary>
        /// Generates a unique album ID
        /// </summary>
        /// <returns>Unique album id</returns>
        public int GenerateAlbumId()
        {
            System.Threading.Interlocked.Increment(ref _hihgestAlbumId);
            return _hihgestAlbumId;
        }
        /// <summary>
        /// Generates a unique track ID
        /// </summary>
        /// <returns>Unique track ID</returns>
        public int GenerateTrackId()
        {
            System.Threading.Interlocked.Increment(ref _hihgestTrackId);
            return _hihgestTrackId;
        }


    }
}
